package com.wy.animation;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * Time:8/3/21
 * Author:ypf
 * Description:
 */
public class ShapeLoadingView extends View {

    private Paint mCirclePaint;
    private Paint mTrianglePaint;
    private Paint mSquarePaint;

    private int mType = 1;

    private ObjectAnimator mRectRoteAnimation;
    private ObjectAnimator mDefaultRoteAnimation;

    public ShapeLoadingView(Context context) {
        this(context, null, 0);
    }

    public ShapeLoadingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShapeLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        initRoteAnimation();
    }

    private void init() {
        mCirclePaint = new Paint();
        mCirclePaint.setColor(getResources().getColor(R.color.circle));

        mTrianglePaint = new Paint();
        mTrianglePaint.setColor(getResources().getColor(R.color.triangle));

        mSquarePaint = new Paint();
        mSquarePaint.setColor(getResources().getColor(R.color.rect));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = getWidth();
        int height = getHeight();

        RectF rect = new RectF(0, 0, width, height);

        switch (mType) {
            case 1:
                canvas.drawOval(rect, mCirclePaint);
                break;
            case 2:
                canvas.drawRect(rect, mSquarePaint);
                break;
            case 3:
                Path path = new Path();
                path.moveTo(width / 2, 0);
                path.lineTo(0, height);
                path.lineTo(width, height);
                path.close();
                //根据Path进行绘制，绘制三角形
                canvas.drawPath(path, mTrianglePaint);
                break;
        }

    }

    public void changeShape() {
        if (1 == mType) {
            mType = 2;
        } else if (2 == mType) {
            mType = 3;
        } else if (3 == mType) {
            mType = 1;
        }
        invalidate();
    }

    /**
     * 在ShapeLoadingView的构造方法中初始化旋转动画即可
     */
    private void initRoteAnimation() {

        mRectRoteAnimation = ObjectAnimator.ofFloat(this,
                "rotation",
                0,
                -120);
        mDefaultRoteAnimation = ObjectAnimator.ofFloat(this,
                "rotation",
                0,
                180);
    }

    /**
     * 得到当前正在上抛时应该旋转的动画
     */
    public ObjectAnimator getUpThrowRoteAnimation() {

        switch (mType) {
            case 2:
                return mRectRoteAnimation;
            default:
                return mDefaultRoteAnimation;
        }
    }

}